package Sort.Sort;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/*  比较插入排序和希尔排序的效率
    由于希尔排序用之前的方法分析时间复杂度很困难，所以就用测试用例比较两者的效率
 */
public class CompareInsertionAndShell {
    public static void main(String[] args) throws IOException {
        //1.创建ArrayList，保存读出来的整数
        ArrayList<Integer> list = new ArrayList<>();

        //2.用BufferedReader读数据，存到ArrayList中
        BufferedReader br = new BufferedReader(new InputStreamReader(CompareInsertionAndShell.class
                .getResourceAsStream("reverseArray.txt")));
        String line = null;
        while ((line = br.readLine()) != null) {
            int i = Integer.parseInt(line);
            list.add(i);
        }
        br.close();

        //3.把list转为数组
        Integer[] array = new Integer[list.size()];
        list.toArray(array);

        //4.测试
        //testShell(array);//如果是100000个数据，shellSort为30ms
        testInsertion(array);//如果是100000个数据，insertionSort为37499ms
    }

    private static void testShell(Integer[] integers) {
        long start = System.currentTimeMillis();
        Sort.shellSort(integers);
        long end = System.currentTimeMillis();
        System.out.println("希尔排序时间是：" + (end - start) + "ms");
    }

    private static void testInsertion(Integer[] integers) {
        long start = System.currentTimeMillis();
        Sort.insertionSort(integers);
        long end = System.currentTimeMillis();
        System.out.println("插入排序时间是：" + (end - start) + "ms");
    }
}
